<div class="bjui-pageContent">
    <form id="business-add" method="post" class="form-horizontal" action="/business/edit" >
        <input type="hidden"  class="input-nm" name="id" id="business-id" value="{{id}}" >
        <div class="form-group">
            <label class="col-lg-12 col-sm-12 control-label"><span class="required-span">*</span>业务名称:
                 
            </label>
            <div class="col-lg-12 col-sm-12">
                <input type="text" class="form-control input-nm" name="name" id="business-name">
            </div>
        </div>
         <div class="form-group">
            <label class="col-lg-12 col-sm-12 control-label">Key:
            </label>
            <div class="col-lg-12 col-sm-12">
                <input type="text" class="form-control input-nm" id="business-key" name="key">
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-12 col-sm-12 control-label">私有协议:
            </label>
            <div class="col-lg-12 col-sm-12">
                <select class="selectpicker show-tick form-control input-nm" id="business-protocolid" name="protocolid" >

                    <option value="" selected  style = 'display：none;' >请选择私有协议</option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-12 col-sm-12 control-label"><span class="required-span">*</span>产品名称:
                 
            </label>
            <div class="col-lg-12 col-sm-12">
                <select class="selectpic+ker show-tick form-control input-nm" id="business-productid"  name="productid" style="height: 36px">
                  <option value="" disabled selected  style = 'display：none;' >请选择产品名称</option>
                </select>
            </div>
        </div>

        
        <div class="form-group">
            <label class="col-lg-12 col-sm-12 control-label">应用名称:</label>
            <div class="col-lg-12 col-sm-12">
                <select class="selectpicker show-tick form-control input-nm" id="business-app" name="appid">
                       <option value="" selected  style = 'display：none;'>请选择应用名称</option>
                </select>
            </div>
        </div>

        <div class="form-group" >
            <label class="col-lg-12 col-sm-12 control-label">业务描述:</label>
            <div class="col-lg-12 col-sm-12">
                <textarea  style="width: 100%;height: 100px" id="business-memo" name="memo" ></textarea>
            </div>
        </div>
    </form>
</div>
<div class="btns-group bjui-pageFooter">
    <button type="button" class="close-btn" >取消</button>
    <button type="submit" class="save-btn">保存</button>
</div>
<script type="text/javascript">
$(function(){
    var id=$("#business-id").val();
    $("#business-protocolid").selectpicker();  //初始化
    get_produce_list();

    if(id){
        get_user_protocol_list(id); 
        get_app_list(id);
        
    }else{
        get_user_protocol_list();
        get_app_list();
        $('#business-key').val(uuid()).attr('readonly',true);
    }
    dialog_close(); 
    $('#business-add').find('[name="productid"]') .selectpicker() .change(function(e) { /* Revalidate the language when it is changed */ 
            $('#business-add').formValidation('revalidateField', 'productid'); 
        }) .end()
        .formValidation({
            framework: 'bootstrap', 
            excluded: ':disabled', 
            trigger:'blur',
            icon: { valid: 'glyphicon glyphicon-ok', 
            invalid: 'glyphicon glyphicon-remove', 
            validating: 'glyphicon glyphicon-refresh' }, 
            fields: {
                name: {
                    validators: {
                        notEmpty: {
                            message: '请填写业务名称'
                        },
                        regexp: {
                            regexp: /^[\u4e00-\u9fa5_a-zA-Z0-9-]{2,30}$/,
                            message: '业务名称只能由字母、数字、中文、下划线和减号组成,长度必须2到30之间'
                        }
                    }
                },
                productid: {
                    validators: {
                        notEmpty: {
                            message: '请选择产品名称'
                        }
                    }
                },
                memo:{
                    validators: {
                        stringLength:{
                          message:'业务描述不得超过200个字符',
                          max:200
                        }
                    }
                }  
            }
        })
        .on('success.form.fv', function(e) {
            // Prevent form submission
            e.preventDefault();
        
            // Get the form instance
            var $form = $(e.target);

            // Get the FormValidation instance
            var bv = $form.data('formValidation');
            // Use Ajax to submit form data
            $.post($form.attr('action'), $form.serialize(), function(result) {
                if(result.statusCode=='200' || result.statusCode=='300'){
                    BJUI.alertmsg('ok',result.message);
                    BJUI.dialog('closeCurrent');
                    BJUI.navtab('refresh', 'sysbusiness')
                }else{
                  BJUI.alertmsg('error',result.message);
                }
            }, 'json');
        });  
})
function get_user_protocol_list(id){

    $.ajax({
        url:"/business/getProtocolList",
        type:"post",
        data:{'id':id},
        dataType:"json",
        success:function(data){
            if(data.statusCode=='200' && data.list !=''){
                $.each(data.list,function(i,item){
                    $("#business-protocolid").append("<option value="+item.id+">"+item.name+"</option>");
                   
                })
            }
            $("#business-protocolid").selectpicker('render');
            $("#business-protocolid").selectpicker('refresh');
            
            
        }
    })
}
function get_business(id){
    $.ajax({
        url:'/business/getBusiness',
        type:'post',
        data:{'id':id},
        dataType:'json',
        success:function(json){
           $("#business-name").val(json.data.name);
           $("#business-memo").val(json.data.memo);
           $('#business-protocolid').val(json.data.protocolid);
           $('#business-protocolid').selectpicker('render');
           $('#business-key').val(json.data.key).attr('readonly',true);
           $("#business-app").val(json.data.appid);
           $('#business-app').selectpicker('render');
           $('#business-productid').val(json.data.productid);
           $('#business-productid').selectpicker('render');
        }
    })
}
function get_produce_list(){
    $.ajax({
        url:'/product/getAllProduct',
        type:'post',
        dataType:'json',
        success:function(data){
            if(data.statusCode=='200'){
                $.each(data.list,function(i,item){
                    $("#business-productid").append('<option value="'+item.id+'" >'+item.name+'</option>');

                })
            }
            $("#business-productid").selectpicker('render');
            $("#business-productid").selectpicker('refresh');
        }
    })
}

function get_app_list(id){
     $.ajax({
        url:"/business/getAppList",
        type:"post",
        data:{'id':id},
        dataType:"json",
        success:function(data){
           
            if(data.statusCode=='200' && data.list !=''){
                $.each(data.list,function(i,item){
                    $("#business-app").append('<option value="'+item.id+'" >'+item.name+'</option>')
                })
            }
            $("#business-app").selectpicker('render');
            $("#business-app").selectpicker('refresh');
            if(id){
               get_business(id); 
            }

        }
     })
   }

</script>
